﻿using Kogel.Dapper.Extension;
using Kogel.Repository;
using Kogel.Repository.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Commons.Infrastructure
{
    public static class IBaseRepositoryExtensions
    {
        // 假设 PageList 和 PageListAsync 是某个类的方法，并且它们是异步的
        public static PageList<T> QueryPage<T>(this IBaseRepository<T> repository, Expression<Func<T, bool>> predicate, int pageIndex, int pageSize)
        {
            return repository.QuerySet().Where(predicate).PageList(pageIndex, pageSize);
        }

        public static PageList<T> QueryPage<T>(this IBaseRepository<T> repository, Expression<Func<T, bool>> predicate, int pageIndex, int pageSize, Expression<Func<T, object>> orderPredicate)
        {
            return repository.QuerySet().Where(predicate).OrderByDescing(orderPredicate).PageList(pageIndex, pageSize);
        }

        public static async Task<PageList<T>> QueryPageAsync<T>(this IBaseRepository<T> repository, Expression<Func<T, bool>> predicate, int pageIndex, int pageSize)
        {
            return await repository.QuerySet().Where(predicate).PageListAsync(pageIndex, pageSize);
        }

        public static async Task<PageList<T>> QueryPageAsync<T>(this IBaseRepository<T> repository, Expression<Func<T, bool>> predicate, int pageIndex, int pageSize, Expression<Func<T, object>> orderPredicate)
        {
            return await repository.QuerySet().Where(predicate).OrderByDescing(orderPredicate).PageListAsync(pageIndex, pageSize);
        }

        public static long InsertIdentity<T>(this IBaseRepository<T> repository, T entity)
        {
            return repository.CommandSet().InsertIdentity(entity);
        }

        public static async Task<long> InsertIdentityAsync<T>(this IBaseRepository<T> repository, T entity)
        {
            return await repository.CommandSet().InsertIdentityAsync(entity);
        }

        public static IEnumerable<T> Query<T>(this IBaseRepository<T> repository, Expression<Func<T, bool>> predicate)
        {
            return repository.QuerySet().Where(predicate).ToList();
        }

        public static async Task<IEnumerable<T>> QueryAsync<T>(this IBaseRepository<T> repository, Expression<Func<T, bool>> predicate)
        {
            return await repository.QuerySet().Where(predicate).ToListAsync();
        }

        public static T QueryFirst<T>(this IBaseRepository<T> repository, Expression<Func<T, bool>> predicate)
        {
            return repository.QuerySet().Where(predicate).Get();
        }

        public static async Task<T> QueryFirstAsync<T>(this IBaseRepository<T> repository, Expression<Func<T, bool>> predicate)
        {
            return await repository.QuerySet().Where(predicate).GetAsync();
        }

    }
}

